package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.ResultMapper;
import com.tanhua.model.domain.Result;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ResultApiImpl implements ResultApi{

    @Autowired
    private ResultMapper resultMapper;

    /**
     * 保存结果
     * @param result
     * @return
     */
    @Override
    public Long save(Result result) {
        resultMapper.insert(result);
        return result.getId();
    }

    /**
     * 根据用户id查询结果
     * @param userId
     * @return
     */
    @Override
    public Result selectByUserId(Long userId) {

        QueryWrapper<Result> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Result result = resultMapper.selectOne(wrapper);

        return result;
    }

    /**
     * 修改测试报告
     * @param result
     * @return
     */
    @Override
    public Long update(Result result) {

        QueryWrapper<Result> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",result.getUserId());

        resultMapper.update(result,wrapper);

        return result.getId();
    }

    /**
     * 根据id查询测试报告
     * @param id
     * @return
     */
    @Override
    public Result selectById(Integer id) {
        Result result = resultMapper.selectById(id);
        return result;
    }

    /**
     * 查询性格相同的好友
     * @param tsId
     * @param characterId
     * @return
     */
    @Override
    public List<Result> selectByIds(Long tsId, Long characterId,Long userId) {

        QueryWrapper<Result> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("ts_id",tsId);
        wrapper1.eq("character_id",characterId);
        wrapper1.notIn("user_id",userId);
        List<Result> results = resultMapper.selectList(wrapper1);

        return results;
    }
}
